class OrderMadesController < ApplicationController
  
  def index
    @order_mades = OrderMade.order('created_at desc')
  end
  
  def new
    @order_made = OrderMade.new
    @order_made.init_order_made_no
    @factories = Factory.being_order_mades
    @unsent_order_made_items = @factories.any? ? OrderMadeItem.unsents_by_factory_id(@factories.first.id)  : []
  end
  
  def create
    @order_made = OrderMade.new(params[:order_made])
    if not @order_made.save
      @factories = Factory.being_order_mades
      @unsent_order_made_items = @factories.any? ? OrderMadeItem.unsents_by_factory_id(@factories.first.id)  : []
      render :new
    else
      # 保存 order_made_items
      params[:order_made_item].each do |order_made_item_attributes|
        @order_made.order_made_items << OrderMadeItem.new(order_made_item_attributes)
      end
      redirect_to @order_made
    end
  end
  
  def unsent_order_made_items
    @unsent_order_made_items = OrderMadeItem.unsents_by_factory_id(params[:factory_id])
  end

  def order_made_items
    @order_made = OrderMade.find(params[:id])
  end
  
  def edit 
    @order_made = OrderMade.find(params[:id])
  end
  
  def update
    @order_made = OrderMade.find(params[:id])
    render :edit if not @order_made.update_attributes(params[:order_made])      
  end
  
  def show
    @order_made = OrderMade.find(params[:id])
  end
  
  def destroy
    @order_made = OrderMade.find(params[:id])
    @order_made.blanked_out!
    respond_to do |format|
      format.html do
        redirect_to order_made_url(@order_made)
      end
      format.js do 
      end
    end
  end
  
  def destroy_order_made_item
    @order_made_item = OrderMadeItem.find(params[:id])
    @order_made_item.destroy
  end

  def new_enter_item
    @order_made_item = OrderMadeItem.find(params[:id])
    @order_made = @order_made_item.order_made
    @enter_item = EnterItem.new(
      :order_id => @order_made_item.order_id,
      :order_item_id => @order_made_item.order_item_id,
      :installation_id => @order_made_item.installation_id,
      :install_item_id => @order_made_item.install_item_id,
      :order_made_id => @order_made_item.order_made_id,
      :order_made_item_id => @order_made_item.id,
      :furniture_id => @order_made_item.furniture_id,
      :entered_count => @order_made_item.order_made_count
    )
  end

  def create_enter_item
    params[:enter_item][:furniture_logistic_stat_id] = FurnitureLogisticStat.find_or_create_by_dept_id_and_furniture_id(params[:dept_id],params[:enter_item][:furniture_id]).id
    @enter_item = EnterItem.new(params[:enter_item])
    render :new_enter_item if not @enter_item.save
  end

  def edit_enter_item
    @enter_item = EnterItem.find(params[:id])
    @order_made_item = @enter_item.order_made_item
    @order_made = @enter_item.order_made
  end

  def update_enter_item
    @enter_item = EnterItem.find(params[:id])
    @order_made_item = @enter_item.order_made_item
    @order_made = @enter_item.order_made
    render :edit_enter_item if not @enter_item.update_attributes(params[:enter_item])
  end

  def destroy_enter_item
    @enter_item = EnterItem.find(params[:id])
    @enter_item.destroy
  end
end
